package com.jianhua.appmarket.common.config;

import com.jianhua.appmarket.common.base.AppResponse;
import com.jianhua.appmarket.common.exception.BussinessException;
import com.jianhua.appmarket.common.exception.TokenTimeoutException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;

/**
 * 自定义异常处理器
 *
 * @author ieflex
 */
@RestControllerAdvice
@Slf4j
public class GobalExceptionHandler {


    /**
     * token 超时异常
     */
    @ExceptionHandler(TokenTimeoutException.class)
    @ResponseBody
    public ResponseEntity tokenTimeoutException(TokenTimeoutException e) {
        return new ResponseEntity(HttpStatus.UNAUTHORIZED);
    }


    /**
     * 拦截所有运行时的全局异常
     */
    @ExceptionHandler(BussinessException.class)
    @ResponseBody
    public AppResponse<?> bussinessException(BussinessException e) {
        return AppResponse.fail(e.getMessage());
    }

    /**
     * 拦截所有运行时的全局异常   
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity runtimeException(Exception e) {
        e.printStackTrace();
        return new ResponseEntity(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
